﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using Motion.MotionPlatform;

namespace Motion.MotionPlatform.Tools
{
	/// <summary>
	/// 用于定位Excel表格中的错误位置信息，根据外部TXT文件定位.
	/// </summary>
	public class ErrorLocation
	{
		//相关Excel表格位置存储处
		private static Dictionary<string, string> _locationDic = new Dictionary<string, string>();

		static ErrorLocation()
		{
			_locationDic.Clear();
			if (!File.Exists(Application.streamingAssetsPath + MotionPlatformData.errorConfiguration))
			{
				Debug.LogError(Application.streamingAssetsPath + MotionPlatformData.errorConfiguration +
				  "，该错误索引文件不存在!");
				return;
			}
			FileStream fStream = new FileStream(Application.streamingAssetsPath +
				  MotionPlatformData.errorConfiguration, FileMode.Open, FileAccess.Read);
			StreamReader fileReader = new StreamReader(fStream);
			string lineReader = fileReader.ReadLine();
			string[] strArray;
			while (lineReader != null)
			{
				strArray = lineReader.Split(',');
				_locationDic.Add(strArray[0] + "-" + strArray[1], strArray[2]);
				//Debug.Log(strArray[0] + "-" + strArray[1] + " -> " + strArray[2]);
				lineReader = fileReader.ReadLine();
			}
			fileReader.Close();
		}

		//返回错误信息位置
		public static string Locate(string key, string sub_key, int column_number)
		{
			string rStr = "";
			if (_locationDic.ContainsKey(key.ToUpper() + "-" + sub_key.ToUpper()))
			{
				rStr = " -> " + key.ToUpper() + " -> " + "( " + (column_number + 2) + ", "
					+ _locationDic[key.ToUpper() + "-" + sub_key.ToUpper()] + " )";
			}
			else
			{
				rStr = " -> " + key.ToUpper() + " -> " + "( " + key.ToUpper() + "-" + sub_key.ToUpper() +
					" is not included in the ErrorLocation.txt file! )";
			}
			return "错误位置：" + MotionPlatformData.dataRootPath + MotionPlatformData.excelName + ".xls" + rStr;
		}

		//返回错误信息位置
		public static string Locate(string key, string sub_key, string id)
		{
			string rStr = "";
			if (_locationDic.ContainsKey(key.ToUpper() + "-" + sub_key.ToUpper()))
			{
				rStr = " -> " + key.ToUpper() + " -> " + "( ID:" + id + ", " + _locationDic[key.ToUpper() +
					"-" + sub_key.ToUpper()] + " )";
			}
			else
			{
				rStr = " -> " + key.ToUpper() + " -> " + "( " + key.ToUpper() + "-" + sub_key.ToUpper() +
					" is not included in the ErrorLocation.txt file! )";
			}
			return "错误位置：" + MotionPlatformData.dataRootPath + MotionPlatformData.excelName + ".xls" + rStr;
		}
	}
}
